package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.imp;

import java.io.IOException;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.DeviceDAO;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.DeviceManager;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.factories.DeviceManagerFactory;

import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.ModbusIOException;
import net.wimpi.modbus.io.ModbusTransport;
import net.wimpi.modbus.msg.ModbusRequest;
import net.wimpi.modbus.msg.ModbusUtil;
import net.wimpi.modbus.msg.WriteSingleRegisterRequest;
import net.wimpi.modbus.msg.WriteSingleRegisterResponse;

public class ActualizationConnectionHandler implements Runnable {

	private ModbusTransport incoming;
	private DeviceManagerFactory deviceManagerFactory;
	private DeviceDAO deviceDAO;
	
	@Override
	public void run() {
		try {
			authenticate();
			incoming.close();
		} catch (IOException e) {
		}
		
	}
	
	public void authenticate(){
		try {

			int dev_nbr = 0;
			DeviceManager deviceManager = null;
			ModbusRequest req =  incoming.readRequest();
			if(req instanceof WriteSingleRegisterRequest){
				if(((WriteSingleRegisterRequest)req).getReference() == 0){
					dev_nbr = ((WriteSingleRegisterRequest)req).getRegister().getValue();
					Device dev = deviceDAO.findDevice(Integer.valueOf(dev_nbr));
					if(dev != null){
						if(dev.getAutoActualization()){
							deviceManager = deviceManagerFactory.create(dev);
							deviceManager.initActualization(incoming,this);
							return;
						}
					}
				}
			}
		
			sendFail();
			
		} catch (ModbusIOException e) {
		}
	
	}
	/**
	 * @return the incoming
	 */
	public ModbusTransport getIncoming() {
		return incoming;
	}
	/**
	 * @param incoming the incoming to set
	 */
	public void setIncoming(ModbusTransport incoming) {
		this.incoming = incoming;
	}
	

	/**
	 * @return the deviceManagerFactory
	 */
	public DeviceManagerFactory getDeviceManagerFactory() {
		return deviceManagerFactory;
	}
	/**
	 * @param deviceManagerFactory the deviceManagerFactory to set
	 */
	public void setDeviceManagerFactory(DeviceManagerFactory deviceManagerFactory) {
		this.deviceManagerFactory = deviceManagerFactory;
	}
	/**
	 * @return the deviceDAO
	 */
	public DeviceDAO getDeviceDAO() {
		return deviceDAO;
	}
	/**
	 * @param deviceDAO the deviceDAO to set
	 */
	public void setDeviceDAO(DeviceDAO deviceDAO) {
		this.deviceDAO = deviceDAO;
	}
	public void sendConfirmation() throws ModbusIOException {
	
		WriteSingleRegisterResponse res = new WriteSingleRegisterResponse(0,1);
		ModbusUtil.setFunctionCode(res, Modbus.WRITE_SINGLE_REGISTER);
		incoming.writeMessage(res);
		
	}
	public void sendFail() throws ModbusIOException {
		
		WriteSingleRegisterResponse res = new WriteSingleRegisterResponse(0,1);
		ModbusUtil.setFunctionCode(res, Modbus.WRITE_SINGLE_REGISTER);
		incoming.writeMessage(res);
		
	}
}
